#include <Starter.hpp>


int Starter::start(){

	Config *config = Config::instance();

	Log::instance().open(config->getLogFile());
	Log::instance() << "Started " << config->getServerName() << std::endl;

	/* Create manager for local TCP/IP Connections */
	ConnectionManager connectionManager;
	/* Create manager for network data */
	IBRCNetworkManager ibrcNetworkManager(&connectionManager);
	/* Create helper for exporting of server information */
	ServerInformation serverInformation(&ibrcNetworkManager);

	/*
	 * Connect to parent server if flag is set
	 */
	if(config->isConnectToServerFlag()){
		/* Initialize connection to parent server */
		connectionManager.initParentServer();
		/* Send SERVER_REGISTRATION message */
		connectionManager.handleOutputData();
	}

	/* Init server for incoming child server connections */
	connectionManager.initChildServer();


	/* main-loop */
	while(true){

		/* Receive data from local TCP/IP connections */
		connectionManager.handleInputData();
		/* Process received messages */
		ibrcNetworkManager.processData();
		/* Transmit data to local TCP/IP connections */
		connectionManager.handleOutputData();


		/* Export current server details to file */
		serverInformation.writeServerInformationFile();

	}


	return 0;
}

